package com.itany.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.ResponseCodeConstant;
import com.itany.nmms.entity.Dept;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.*;
import com.itany.nmms.service.DeptService;
import com.itany.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo,String pageSize){
        ModelAndView mav=new ModelAndView();
        PageInfo<Dept> deptPage=deptService.findPage(pageNo,pageSize);
        mav.addObject("deptPage",deptPage);
        mav.setViewName("backend/deptManager");
        return mav;
    }
    @RequestMapping("/addFatherDept")
    public ResponseResult addFatherDept(String name, String remark, HttpSession session) {
        ResponseResult result = new ResponseResult();
        Staff staff = (Staff) session.getAttribute("staff");
        try {
            deptService.addFatherDept(name, remark, staff);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (LoginDisabledException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_DISABLED);
        } catch (DeptExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/addSonDept")
    public ResponseResult addSonDept(String fatherId, String name, String remark, HttpSession session) {
        ResponseResult result = new ResponseResult();
        Staff staff = (Staff) session.getAttribute("staff");
        try {
            deptService.addSonDept(fatherId, name, remark, staff);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (LoginDisabledException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_DISABLED);
        } catch (DeptExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/findById")
    public ResponseResult findById(String id){
        ResponseResult result=new ResponseResult();
        try {
            Dept dept=deptService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(dept);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyDept")
    public ResponseResult modifyDept(String id, String name, String remark){
        ResponseResult result=new ResponseResult();
        try {
            deptService.modifyDept(id,name,remark);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (DeptExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


@RequestMapping("/enabledDept")
public ResponseResult enabledDept(String id){
        ResponseResult result=new ResponseResult();
    try {
        deptService.enabledDept(id);
        result.setMessage("成功");
        result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
    } catch (RequestParameterErrorException e) {
        result.setMessage(e.getMessage());
        result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
    } catch (DeptStatusException e) {
        result.setMessage(e.getMessage());
        result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
    }catch (Exception e) {
        e.printStackTrace();
        result.setMessage("服务器内部异常");
        result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
    }

    return result;
}

    @RequestMapping("/disabledDept")
    public ResponseResult disabledDept(String id){
        ResponseResult result=new ResponseResult();
        try {
            deptService.disabledDept(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
